package com.person.title.service.impl;

import java.util.List;

import com.person.commons.utils.DAO;
import com.person.sys.model.SysFunc;
import com.person.title.model.Function;
import com.person.title.model.Title;
import com.person.title.model.TitleFunc;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleFuncService;

public class TitleFuncServiceImpl implements TitleFuncService {

	private DAO dao ;
	
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';//主功能分类代码重复的提示返回
	private static final char FAIL_2 = '3';//主功能分类名字重复的提示返回
	
	@Override
	public List<TitleFunc> TitleFuncList(String funcId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char createTitleFunc(TitleFunc titleFunc) {
				
		char forward = 0;
		String code = titleFunc.getClassCodeGroup() ;
		code = code.substring(0, code.length()-2) ;
		
		//分类代码是否重复
		String hql1 = "from TitleFunc where classCodeGroup = '"+titleFunc.getClassCodeGroup()+"'";
		List<TitleFunc> list1 = dao.find(hql1);
		
		//名字是否重复
		String hql2 = "from TitleFunc where classCodeGroup like '"+code+"__' and funcName = '"+titleFunc.getFuncName()+"'";
		List<TitleFunc>  list2 = dao.find(hql2);

		if(list1.size() == 0 && list2.size() == 0){			
			this.dao.save(titleFunc);
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}
		}
		return forward;
	}

	@Override
	public char deleteTitleFunc(TitleFunc titleFunc) {
		
		String hql = "from TitleFunc where classCodeGroup like '" + titleFunc.getClassCodeGroup() + "%'";
	    List<TitleFunc> list = dao.find(hql);
	    
	    if(list.size() == 1){
	    	this.dao.delete(titleFunc) ;	    		    	
	    	return SUCCESS;
	    }else{
	    	
	    	return FAIL;
	    }
	}

	@Override
	public List findTitleFuncByCode(String[] classCodeGroup) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TitleFunc findTitleFuncById(String funcId) {
		
		return (TitleFunc) this.dao.find(TitleFunc.class, funcId) ;
	}

	@Override
	public List<Function> functionSelectList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char modifyTitleFunc(TitleFunc titleFunc) {
		
		char forward = 0;
		String code = titleFunc.getClassCodeGroup() ;
		code = code.substring(0, code.length()-2) ;
		
		//分类代码是否重复
		String hql1 = "from TitleFunc where classCodeGroup = '"+titleFunc.getClassCodeGroup()+"' and funcId != '"+titleFunc.getFuncId()+"'";
		List<TitleFunc> list1 = dao.find(hql1);
				
		//名字是否重复
		String hql2 = "from TitleFunc where classCodeGroup like '"+code+"__' and funcName = '"+titleFunc.getFuncName()+"' and funcId != '"+titleFunc.getFuncId()+"'";
		List<Title>  list2 = dao.find(hql2);

		if(list1.size() == 0 && list2.size() == 0){
			
			dao.update(titleFunc);
				
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}
		}
		return forward;
	}
	
	@Override
	public char modifyJuniorTitleFunc(TitleFunc titleFunc, String classCodeGroup) {
		String hql = "from TitleFunc where classCodeGroup like '" + classCodeGroup + "__%' order by classCodeGroup " ;
		List <TitleFunc> titleFuncs = this.dao.find(hql) ;	
		
		for (TitleFunc titleFunc1:titleFuncs) {
			classCodeGroup = titleFunc1.getClassCodeGroup() ;
			String classCodeGroup1 = titleFunc.getClassCodeGroup() ;
			classCodeGroup1 = classCodeGroup1 + classCodeGroup.substring(classCodeGroup1.length(), classCodeGroup.length()) ;
			titleFunc1.setClassCodeGroup(classCodeGroup1) ;
			
			this.dao.update(titleFunc1) ;
		}
		return SUCCESS ;
	}
	
	
	@Override
	public List<TitleFunc> juniorTitleFuncList(String funcId) {

		TitleFunc titleFunc =(TitleFunc) this.dao.find(TitleFunc.class, funcId) ;
		String hql = "from TitleFunc where classCodeGroup like '" + titleFunc.getClassCodeGroup() + "__' order by classCodeGroup " ;
		List <TitleFunc> titleFuncs = this.dao.find(hql) ;	
		return titleFuncs;
	}
	
	@Override
	public TitleFunc findTitleFuncByCode(String classCode) {
		String hql = "from TitleFunc where classCodeGroup = '" + classCode+ "'" ;
		List titleFuncs = this.dao.find(hql) ;
		TitleFunc titleFunc = null ;
		if (titleFuncs.size()>0) {
			titleFunc = (TitleFunc) titleFuncs.get(0) ;
		}
		return titleFunc ;
	}
	
	

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	
}
